﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Winning.SolutionManager
{
    public partial class FormReferenceUpdate : Form
    {
        public int Type { get; set; }
        public string Reference { get; set; }

        private Tuple<int, string> backup = null;
        private FormReferenceUpdate()
        {
            InitializeComponent();
        }

        public FormReferenceUpdate(int type, string assembly,string project):this()
        {
            this.backup = new Tuple<int,string>(type,type == 1 ? project : assembly);
            this.Type = type;
            this.radProject.Checked = type == 1;
            this.radAssembly.Checked = type == 2;
            this.linkLabel1.Text = project ?? string.Empty;
            this.txtAssembly.Text = assembly ?? string.Empty;

            if ((project == null || project.Length == 0))
            {
                this.radProject.Enabled = false;
                this.radProject.Checked = false;
                this.Type = 2;
                this.radAssembly.Checked = true;
            }

            if(this.Type == 2)
            {
                this.Reference = assembly;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (radProject.Checked) this.Type = 1;
            else if (radAssembly.Checked) this.Type = 2;

            if (this.Type == 1) this.Reference = linkLabel1.Text;
            else if (this.Type == 2) this.Reference = txtAssembly.Text;
            if (this.Type == 2 && !System.IO.File.Exists(this.Reference)) errorProvider1.SetError(txtAssembly, "无效的程序集路径。");
            else if (this.backup.Item1 == this.Type && this.backup.Item2 == this.Reference) this.DialogResult = DialogResult.Cancel;
            else this.DialogResult = DialogResult.OK;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            string file = System.IO.Path.GetFileName(this.txtAssembly.Text.Trim());
            dialog.Filter = " 程序集|" + file;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) this.txtAssembly.Text = dialog.FileName;
        }

        private void radAssembly_CheckedChanged(object sender, EventArgs e)
        {
            btnSelect.Enabled = radAssembly.Checked;
        }
    }
}
